﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamAnalysis.Model;
using System.Web.Security;
using ExamAnalysis.Core;
using ExamAnalysis.Core.Statistics;
using Ext.Net;
using System.IO;


namespace ExamAnalysis.Reports
{
    public partial class PicReport : System.Web.UI.Page
    {
        static int ExamID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            //string teacher = "1";
            string teacher = Membership.GetUser().UserName;
            if(!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    List<ExamInfo> ei = null;
                    if (Request.QueryString["role"] == null)
                    {
                        ei = RoleData.GetExamInfoByUserName(Membership.GetUser().UserName);
                    }
                    else if (Roles.GetRolesForUser().Contains(Request.QueryString["role"]))
                        ei = RoleData.GetExamInfoOfRole(Membership.GetUser().UserName, Request.QueryString["role"]);
                    List<ClassInfo> ci = null;
                    if (Request.QueryString["role"] == null)
                    {
                        ci = RoleData.GetClassByUserName(Membership.GetUser().UserName);
                    }
                    else if (Roles.GetRolesForUser().Contains(Request.QueryString["role"]))
                        ci = RoleData.GetClassOfRole(Membership.GetUser().UserName, Request.QueryString["role"]);
                    var score = (from s in db.StudentInExam
                                 join st in db.StudentInfo on s.StudentID equals st.StudentID
                                 join t in ci on st.ClassID equals t.ClassID
                                 join et in ei on s.ExamID equals et.ExamID
                                 select new { ExamID = s.ExamID, ExamName = s.ExamInfo.ExamName, ClassID = st.ClassID, ClassName = st.ClassInfo.ClassName }).Distinct();
                    Store1.DataSource = score;
                    Store1.DataBind();
                    if (ExamID != 0)
                    {
                        Table.Visible = true;
                        var score2 = from s in db.StudentInExam
                                     orderby s.Score
                                     join st in db.StudentInfo on s.StudentID equals st.StudentID
                                     join t in ci on st.ClassID equals t.ClassID
                                     join et in ei on s.ExamID equals et.ExamID
                                     where s.ExamID == ExamID
                                     select new { Student = s.StudentInfo.StudentName, Score = s.Score, StudentID = s.StudentID };
                        foreach (var sco in score2)
                        {
                            IntoLable(Convert.ToInt32(sco.Score));
                        }

                        var score3 = from ss in score2
                                     orderby ss.Score
                                     select ss.Score;
                        decimal mean = score3.Average();
                        decimal mid = score3.ToList()[score3.Count() / 2];
                        List<object> stat = null;
                        if (mean > mid)
                        {
                            stat = new List<object> 
                     {
                        new {Max = score3.Max()-1-score3.Average(),Min=score3.Min()-1,Mean=score3.Average()-1-mid,Mid=mid-1-score3.Min(),MinScore=1,MidScore=1,MeanScore=1,MaxScore=1},
                     };
                        }
                        else
                        {
                            stat = new List<object> 
                     {
                        new {Max = score3.Max()-1-score3.Average(),Min=score3.Min()-1,Mid=mid-score3.Average()-1,Mean=mean-1-score3.Min(),MidScore=1,MinScore=1,MeanScore=1,MaxScore=1},
                     };
                        }
                        Label11.Text = "最大值：" + score3.Max() + "  最小值：" + score3.Min() + "  平均值：" + mean + "  中位数：" + mid;
                        Store2.DataSource = stat;
                        Store2.DataBind();
                    }
                    else
                    {
                        Table.Visible = false;
                    }
                }

            }
        }


        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (ComboBox1.SelectedItem != null)
            {
                ExamID = Convert.ToInt32(ComboBox1.SelectedItem.Value);
                Response.Redirect("PicReport.aspx");
            }
            else
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个考试")).Show();
            }

        }

        protected void IntoLable(int i)
        {
            switch (i / 10)
            {
                case 0: Label0.Text += " " + i % 10 + " ";
                    break;
                case 1: Label1.Text += " " + i % 10 + " ";
                    break;
                case 2: Label2.Text += " " + i % 10 + " ";
                    break;
                case 3: Label3.Text += " " + i % 10 + " ";
                    break;
                case 4: Label4.Text += " " + i % 10 + " ";
                    break;
                case 5: Label5.Text += " " + i % 10 + " ";
                    break;
                case 6: Label6.Text += " " + i % 10 + " ";
                    break;
                case 7: Label7.Text += " " + i % 10 + " ";
                    break;
                case 8: Label8.Text += " " + i % 10 + " ";
                    break;
                case 9: Label9.Text += " " + i % 10 + " ";
                    break;
                default: Label10.Text += " " + i % 10 + " ";
                    break;
            }
        }
    }
}